package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 权限管理项
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    //dubbo查找服务
    //定义服务接口
    @Reference
    private PermissionService permissionService;

    //新增检查项
    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('PERMISSION_ADD')")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.ADD_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.ADD_PERMISSION_SUCCESS);
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('PERMISSION_DETELE')")
    public Result delete(Integer id) {
        try {
            permissionService.deletePermissionById(id);
            permissionService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('PERMISSION_EDIT')")
    public Result edit(@RequestBody Permission permission) {
        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_PERMISSION_SUCCESS);
    }


    @RequestMapping("/findById")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public Result findById(Integer id) {
        Permission permission = null;
        try {
            permission = permissionService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.QUERY_PERMISSION_SUCCESS, permission);
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('PERMISSION_QUERY')")
    public PageResult find(@RequestBody QueryPageBean queryPageBean) {
        PageInfo<Permission> pageInfo = permissionService.selectByCondition(queryPageBean);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }

}
